package com.erpanderp.wam.ac.view.reimbursement;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.erpanderp.wam.ac.model.entity.reimbursement.TravelExpense;
import com.erpanderp.wam.ac.model.service.reimbursement.TravelReimbursementDAO;
import com.erpanderp.wam.usm.model.entity.user.WAMUser;
import com.erpanderp.wam.util.CommonLookup;

@ManagedBean
@ViewScoped
public class InprocessTravelExpensesBean implements Serializable{

	
	private static final long serialVersionUID = 1L;
	HttpSession session = (HttpSession)FacesContext.getCurrentInstance().getExternalContext().getSession(true);
	private WAMUser wamUser = (WAMUser) session.getAttribute(CommonLookup.SESSION_ATTRIBUTE_WAMUSER);
	HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
	ServletContext context = (ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext();

	private List<TravelExpense> travelExpenseInprocess = new ArrayList<TravelExpense>();
	private List<TravelExpense> travelExpenseRecommended = new ArrayList<TravelExpense>();
	private List<TravelExpense> travelExpenseRejected = new ArrayList<TravelExpense>();
	
	private TravelReimbursementDAO travelReimbursementDAO=new TravelReimbursementDAO();
	
	private int inprocessSNo=1;
	private int recommendedSNo=1;
	private int rejectedSNo=1;
	
	public InprocessTravelExpensesBean() {
		
		travelExpenseInprocess = travelReimbursementDAO.getTravelExpenseRequestsByRecommenderId(wamUser.getEmployee().getEmployeeId(), "Inprocess");
		for(TravelExpense travelExpense  : travelExpenseInprocess){
			travelExpense.setsNo(inprocessSNo);
			inprocessSNo++;
		}
		
		travelExpenseRecommended = travelReimbursementDAO.getTravelExpenseRequestsByRecommenderId(wamUser.getEmployee().getEmployeeId(), "Recommended");
		for(TravelExpense travelExpense  : travelExpenseRecommended){
			travelExpense.setsNo(recommendedSNo);
			recommendedSNo++;
		}
		
		travelExpenseRejected = travelReimbursementDAO.getTravelExpenseRequestsByRecommenderId(wamUser.getEmployee().getEmployeeId(), "Rejected");
		for(TravelExpense travelExpense  : travelExpenseRejected){
			travelExpense.setsNo(rejectedSNo);
			rejectedSNo++;
		}
	}
	
	//Recommended by recommender 
		 public String travelExpenseRecommendAction() {
			 System.out.println("=====INSIDE travelExpenseRecommendAction() method=======");
			 try{
			  Map<String,String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
			  
			  String travelCode = params.get("travelCode");
			  String expenseCode = params.get("expenseCode");
			  String employeeCode = params.get("employeeCode");
			  String activity = params.get("activity");
			  
			  FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put("travelCode", travelCode);
			  FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put("expenseCode", expenseCode);	
			  FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put("employeeCode", employeeCode);	
			  FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put("activity", activity);	
			  
			  FacesContext.getCurrentInstance().getExternalContext().redirect("/wam/ac/reimbursement/approvetravelexpense.html?travelCode="+travelCode+"&expenseCode="+expenseCode+"&employeeCode="+employeeCode+"&activity="+activity);
			
			 
			  }catch (Exception e) {
				  e.printStackTrace();
			 }
			 return "createtravelreimbursement";
		 
			}

	
	//SETTERS AND GETTERS
	public WAMUser getWamUser() {
		return wamUser;
	}

	public void setWamUser(WAMUser wamUser) {
		this.wamUser = wamUser;
	}


	public List<TravelExpense> getTravelExpenseInprocess() {
		return travelExpenseInprocess;
	}


	public void setTravelExpenseInprocess(List<TravelExpense> travelExpenseInprocess) {
		this.travelExpenseInprocess = travelExpenseInprocess;
	}


	public TravelReimbursementDAO getTravelReimbursementDAO() {
		return travelReimbursementDAO;
	}


	public void setTravelReimbursementDAO(TravelReimbursementDAO travelReimbursementDAO) {
		this.travelReimbursementDAO = travelReimbursementDAO;
	}

	public int getInprocessSNo() {
		return inprocessSNo;
	}

	public void setInprocessSNo(int inprocessSNo) {
		this.inprocessSNo = inprocessSNo;
	}

	public int getRejectedSNo() {
		return rejectedSNo;
	}

	public void setRejectedSNo(int rejectedSNo) {
		this.rejectedSNo = rejectedSNo;
	}

	public List<TravelExpense> getTravelExpenseRejected() {
		return travelExpenseRejected;
	}

	public void setTravelExpenseRejected(List<TravelExpense> travelExpenseRejected) {
		this.travelExpenseRejected = travelExpenseRejected;
	}

	public List<TravelExpense> getTravelExpenseRecommended() {
		return travelExpenseRecommended;
	}

	public void setTravelExpenseRecommended(List<TravelExpense> travelExpenseRecommended) {
		this.travelExpenseRecommended = travelExpenseRecommended;
	}

}
